package com.zenithmind.version.controller;

import com.zenithmind.common.result.PageResult;
import com.zenithmind.common.result.Result;
import com.zenithmind.version.controller.handler.RepositoryRequestHandler;
import com.zenithmind.version.pojo.dto.RepositoryCreateDTO;
import com.zenithmind.version.pojo.dto.RepositoryStatisticsDTO;
import com.zenithmind.version.pojo.vo.RepositoryVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 仓库控制器 - 遵循单一职责原则，专门处理HTTP请求路由
 *
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Tag(name = "仓库管理", description = "仓库相关接口")
@RestController
@RequestMapping("/api/v1/repositories")
@RequiredArgsConstructor
public class RepositoryController {

    private final RepositoryRequestHandler requestHandler;

    @Operation(summary = "创建仓库", description = "创建新的代码仓库")
    @PostMapping
    public Result<RepositoryVO> createRepository(@Valid @RequestBody RepositoryCreateDTO createDTO) {
        return requestHandler.handleCreateRepository(createDTO);
    }

    @Operation(summary = "分页查询仓库列表", description = "分页查询仓库列表")
    @GetMapping
    public Result<PageResult<RepositoryVO>> getRepositoryPage(
            @Parameter(description = "当前页") @RequestParam(defaultValue = "1") Long current,
            @Parameter(description = "页大小") @RequestParam(defaultValue = "10") Long size,
            @Parameter(description = "所有者ID") @RequestParam(required = false) String ownerId,
            @Parameter(description = "仓库类型") @RequestParam(required = false) Integer type,
            @Parameter(description = "仓库状态") @RequestParam(required = false) Integer status,
            @Parameter(description = "关键词") @RequestParam(required = false) String keyword) {
        return requestHandler.handleGetRepositoryPage(current, size, ownerId, type, status, keyword);
    }

    @Operation(summary = "查询仓库详情", description = "根据ID查询仓库详情")
    @GetMapping("/{id}")
    public Result<RepositoryVO> getRepositoryById(@Parameter(description = "仓库ID") @PathVariable String id) {
        return requestHandler.handleGetRepositoryById(id);
    }

    @Operation(summary = "根据名称和所有者查询仓库", description = "根据仓库名称和所有者查询仓库")
    @GetMapping("/{ownerName}/{repositoryName}")
    public Result<RepositoryVO> getRepositoryByNameAndOwner(
            @Parameter(description = "所有者名称") @PathVariable String ownerName,
            @Parameter(description = "仓库名称") @PathVariable String repositoryName) {
        return requestHandler.handleGetRepositoryByNameAndOwner(repositoryName, ownerName);
    }

    @Operation(summary = "更新仓库信息", description = "更新仓库基本信息")
    @PutMapping("/{id}")
    public Result<Boolean> updateRepository(
            @Parameter(description = "仓库ID") @PathVariable String id,
            @Valid @RequestBody RepositoryCreateDTO updateDTO) {
        return requestHandler.handleUpdateRepository(id, updateDTO);
    }

    @Operation(summary = "删除仓库", description = "删除指定仓库")
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteRepository(@Parameter(description = "仓库ID") @PathVariable String id) {
        return requestHandler.handleDeleteRepository(id);
    }

    @Operation(summary = "归档仓库", description = "归档指定仓库")
    @PostMapping("/{id}/archive")
    public Result<Boolean> archiveRepository(@Parameter(description = "仓库ID") @PathVariable String id) {
        return requestHandler.handleArchiveRepository(id);
    }

    @Operation(summary = "取消归档仓库", description = "取消归档指定仓库")
    @PostMapping("/{id}/unarchive")
    public Result<Boolean> unarchiveRepository(@Parameter(description = "仓库ID") @PathVariable String id) {
        return requestHandler.handleUnarchiveRepository(id);
    }

    @Operation(summary = "Fork仓库", description = "Fork指定仓库")
    @PostMapping("/{id}/fork")
    public Result<RepositoryVO> forkRepository(
            @Parameter(description = "源仓库ID") @PathVariable String id,
            @Parameter(description = "新仓库名称") @RequestParam String name) {
        return requestHandler.handleForkRepository(id, name);
    }

    @Operation(summary = "查询用户仓库列表", description = "查询指定用户的仓库列表")
    @GetMapping("/user/{userId}")
    public Result<List<RepositoryVO>> getUserRepositories(
            @Parameter(description = "用户ID") @PathVariable String userId,
            @Parameter(description = "仓库类型") @RequestParam(required = false) Integer type) {
        return requestHandler.handleGetUserRepositories(userId, type);
    }

    @Operation(summary = "获取仓库统计信息", description = "获取用户的仓库统计信息")
    @GetMapping("/statistics")
    public Result<RepositoryStatisticsDTO> getRepositoryStatistics(
            @Parameter(description = "所有者ID") @RequestParam String ownerId) {
        return requestHandler.handleGetRepositoryStatistics(ownerId);
    }
}
